{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# olympic"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/olympic.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/olympic.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Olympic puzzle in Google CP Solver.\n",
    "\n",
    "  Benchmark for Prolog (BProlog)\n",
    "  '''\n",
    "  File   : olympic.pl\n",
    "  Author : Neng-Fa ZHOU\n",
    "  Date   : 1993\n",
    "\n",
    "  Purpose: solve a puzzle taken from Olympic Arithmetic Contest\n",
    "\n",
    "  Given ten variables with the following configuration:\n",
    "\n",
    "                 X7   X8   X9   X10\n",
    "\n",
    "                    X4   X5   X6\n",
    "\n",
    "                       X2   X3\n",
    "\n",
    "                          X1\n",
    "\n",
    "  We already know that X1 is equal to 3 and want to assign each variable\n",
    "  with a different integer from {1,2,...,10} such that for any three\n",
    "  variables\n",
    "                        Xi   Xj\n",
    "\n",
    "                           Xk\n",
    "  the following constraint is satisfied:\n",
    "\n",
    "                      |Xi-Xj| = Xk\n",
    "  '''\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/olympic.mzn\n",
    "  * SICStus Prolog: http://www.hakank.org/sicstus/olympic.pl\n",
    "  * ECLiPSe: http://hakank.org/eclipse/olympic.ecl\n",
    "  * Gecode: http://hakank.org/gecode/olympic.cpp\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def minus(solver, x, y, z):\n",
    "  solver.Add(z == abs(x - y))\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver('Olympic')\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 10\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  Vars = [solver.IntVar(1, n, 'Vars[%i]' % i) for i in range(n)]\n",
    "  X1, X2, X3, X4, X5, X6, X7, X8, X9, X10 = Vars\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(solver.AllDifferent(Vars))\n",
    "\n",
    "  solver.Add(X1 == 3)\n",
    "  minus(solver, X2, X3, X1)\n",
    "  minus(solver, X4, X5, X2)\n",
    "  minus(solver, X5, X6, X3)\n",
    "  minus(solver, X7, X8, X4)\n",
    "  minus(solver, X8, X9, X5)\n",
    "  minus(solver, X9, X10, X6)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  db = solver.Phase(Vars, solver.INT_VAR_SIMPLE, solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print('Vars:', [Vars[i].Value() for i in range(n)])\n",
    "\n",
    "  print()\n",
    "  print('num_solutions:', num_solutions)\n",
    "  print('failures:', solver.Failures())\n",
    "  print('branches:', solver.Branches())\n",
    "  print('WallTime:', solver.WallTime(), 'ms')\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
